package com.hyjy.music.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.media.AudioManager

// 音量变化时调用
typealias OnMediaVolumeChange = Int.() -> Unit

/**
 * 媒体音量变化的广播接收者
 */
class MediaVolumeReceiver(private val listener: OnMediaVolumeChange) : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if ("android.media.VOLUME_CHANGED_ACTION" == intent.action) {
            // 对外暴露当前的媒体音量大小
            val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
            val currVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
            listener.invoke(currVolume)
        }
    }
}